package geppetto.cat.programs;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentSymetrization;
import geppetto.cat.common.StaticTools;
import geppetto.cat.corpus.BilingualCorpus;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;



/**
 * This class gets a file with the output of GizaM4 results
 * loads them and evaluates and prints them.
 * @author javg
 *
 */
public class createGizaSymmetricModels {

	public static void main(String[] args) throws IOException {
		System.out.println("Start main");
		String corpusDescription = args[0];
		int size = Integer.parseInt(args[1]); // Needs to be at most as much as the gold cor
		int setSize = Integer.parseInt(args[2]);
		String gizaDir = args[3];
		String outputDir = args[4];
		System.out.println("Size " + size);
		System.out.println("setSize " + setSize);
		System.out.println("GizaDir " + gizaDir);
		System.out.println("OutputDir " + outputDir);
	

		
		BilingualCorpus forwardCorpus = BilingualCorpus.getCorpusFromFileDescription(
				corpusDescription, size);
		BilingualCorpus backwardCorpus = forwardCorpus.reverse(); 
		String forwardFile = gizaDir + "giza."+ forwardCorpus.getForeignSufix()+"-"+forwardCorpus.getSourceSufix() + "/" +
		forwardCorpus.getForeignSufix()+"-"+forwardCorpus.getSourceSufix() +".A3.final";
		BufferedReader gizaForward = new BufferedReader(new FileReader(forwardFile));
		String backwardFile = gizaDir + "giza."+ backwardCorpus.getForeignSufix()+"-"+backwardCorpus.getSourceSufix() + "/" +
		backwardCorpus.getForeignSufix()+"-"+backwardCorpus.getSourceSufix() +".A3.final";
		BufferedReader gizaBackward = new BufferedReader(new FileReader(backwardFile));
		
		String forwardDir = outputDir + "/forward/" ;
		StaticTools.createDir(forwardDir);
		PrintStream forwardStream =  new PrintStream(new FileOutputStream(forwardDir+"aligned"), true, "UTF-8");
		String backwardDir = outputDir + "/backward/" ;
		StaticTools.createDir(backwardDir);
		PrintStream backwardStream =  new PrintStream(new FileOutputStream(backwardDir + "aligned"), true, "UTF-8");
		String intersectionDir = outputDir + "/intersection/" ;
		StaticTools.createDir(intersectionDir);
		PrintStream intersectionStream =  new PrintStream(new FileOutputStream(intersectionDir + "aligned"), true, "UTF-8");
		String gdfDir = outputDir + "/growDiagFinal/" ;
		StaticTools.createDir(gdfDir);
		PrintStream growDiagFinalStream =  new PrintStream(new FileOutputStream(gdfDir + "aligned"), true, "UTF-8");
		
		
		
		

		for(int i = 0; i < forwardCorpus.getNumberOfTrainingSentences(); i++){
			String fline1 =  gizaForward.readLine();
			String fline2 = gizaForward.readLine();
			String fline3 = gizaForward.readLine();
			String bline1 = gizaBackward.readLine();
			String bline2 = gizaBackward.readLine();
			String bline3 = gizaBackward.readLine();
			Alignment forward = Alignment.fromGizaFormat(forwardCorpus, i, BilingualCorpus.TRAIN_CORPUS, fline1, fline2, fline3);			
			Alignment backward  = Alignment.fromGizaFormat(backwardCorpus, i, BilingualCorpus.TRAIN_CORPUS, bline1, bline2, bline3);
			Alignment intersection = AlignmentSymetrization.intersect(forward, backward);
			Alignment growdiagfinal = AlignmentSymetrization.growDiagFinal(forward, backward);
			forwardStream.println(forward.toMosesFormat(forwardCorpus)[0]);
			backwardStream.println(backward.toMosesFormat(backwardCorpus)[0]);
			intersectionStream.println(intersection.toMosesFormat(forwardCorpus)[0]);
			growDiagFinalStream.println(growdiagfinal.toMosesFormat(forwardCorpus)[0]);
		}
		
		
		
		
		
			
	}
	
}
